package com.basic.library.base

import android.view.KeyEvent
import android.view.View
import android.widget.FrameLayout
import com.basic.library.BR
import com.basic.library.R
import com.basic.library.databinding.BaseActivityWebviewBinding
import com.just.agentweb.AgentWeb

/**
 * @author yj
 * @description
 */
class WebViewActivity : BaseActivity<BaseActivityWebviewBinding, BaseViewModel>() {

    lateinit var mAgentWeb: AgentWeb
    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun getContentViewResId(): Int {
        return R.layout.base_activity_webview
    }

    override fun initView() {
        setBack(null)
        intent.getStringExtra("webTitle").run {
            if (this.isNullOrEmpty())
                binding.incWebView.baseTitleBar.visibility = View.GONE
            setTitle(this)
        }
        mAgentWeb = AgentWeb.with(this) //传入Activity
            .setAgentWebParent(
                binding.fWebView,
                FrameLayout.LayoutParams(-1, -1)
            ) //传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams
            .useDefaultIndicator()
            .createAgentWeb()
            .ready()
            .go(intent.getStringExtra("webUrl"))
    }

    override fun onNoDoubleClick(v: View) {}

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            true
        } else super.onKeyDown(keyCode, event)
    }

    override fun onPause() {
        mAgentWeb.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onResume() {
        mAgentWeb.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        mAgentWeb.webLifeCycle.onDestroy()
        super.onDestroy()
    }
}